package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sc.nft.entity.MallOrder;
import com.sc.nft.entity.RefundRecordVO;
import com.sc.nft.entity.vo.mall.MallOrderStatusVO;
import com.sc.nft.enums.MallOrderRefundStatusEnum;
import com.sc.nft.enums.MallOrderStatusEnum;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.entity.MallOrderStatusLog;
import com.sc.nft.mapper.MallOrderStatusLogMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 订单操作记录表(sc_mall_order_status_log)数据DAO
 *
 * @author Sun
 * @since 2023-05-09 10:47:20
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class MallOrderStatusLogDao extends ServiceImpl<MallOrderStatusLogMapper, MallOrderStatusLog> {

    public List<MallOrderStatusLog> getListByOrderId(Long orderId) {
        LambdaQueryWrapper<MallOrderStatusLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallOrderStatusLog::getMallOrderId, orderId);
        wrapper.eq(MallOrderStatusLog::getIsRefund, Boolean.FALSE);
        return list(wrapper);
    }

    public MallOrderStatusLog getOneByOrderIdAndChangeType(Long id, String changeType) {
        LambdaQueryWrapper<MallOrderStatusLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallOrderStatusLog::getMallOrderId, id);
        wrapper.eq(MallOrderStatusLog::getChangeType, changeType);
        wrapper.last("limit 1");
        wrapper.orderByDesc();
        return getOne(wrapper);
    }

    public List<MallOrderStatusLog> getListByOrderIdAndRefund(Long orderId ,Boolean isRefund) {
        LambdaQueryWrapper<MallOrderStatusLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallOrderStatusLog::getMallOrderId, orderId);
        wrapper.eq(MallOrderStatusLog::getIsRefund, isRefund);
        return list(wrapper);

    }

    public List<MallOrderStatusLog> getListByOrderIdGroupType(Long orderId) {
        LambdaQueryWrapper<MallOrderStatusLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallOrderStatusLog::getMallOrderId, orderId);
        wrapper.in(MallOrderStatusLog::getChangeType, MallOrderStatusEnum.WAIT_RECEIPT.name(),MallOrderStatusEnum.TO_BE_RECEIPT.name(),MallOrderStatusEnum.FINISH.name());
        wrapper.groupBy(MallOrderStatusLog::getChangeType);
        wrapper.orderByAsc(MallOrderStatusLog::getId);
        return list(wrapper);
    }

    public List<MallOrderStatusLog> getListByOrderIdsAndChangeType(List<Long> orderIds,String changeType) {
        LambdaQueryWrapper<MallOrderStatusLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(MallOrderStatusLog::getMallOrderId, orderIds);
        wrapper.in(MallOrderStatusLog::getChangeType, changeType);
        return list(wrapper);
    }

    public List<MallOrderStatusLog> getFinishSevenDaysData() {
        return baseMapper.getFinishSevenDaysData();
    }

    public List<MallOrderStatusLog> getMiniFinishSevenDaysData() {
        return baseMapper.getMiniFinishSevenDaysData();
    }

    public void updateIsSettleByOrderId(Long id) {
        LambdaUpdateWrapper<MallOrderStatusLog> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(MallOrderStatusLog::getId, id);
        wrapper.eq(MallOrderStatusLog::getChangeType, MallOrderStatusEnum.FINISH.name());
        wrapper.eq(MallOrderStatusLog::getIsSettle, Boolean.FALSE);
        wrapper.set(MallOrderStatusLog::getIsSettle, Boolean.TRUE);
        update(new MallOrderStatusLog(),wrapper);
    }
}